package com.ruoyi.repairCar.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 生产者配置（救援请求发起方）
@Configuration
public class RescueProducerConfig {
    // 最大重试范围（示例：3km）
    private static final int MAX_RETRY_RANGE = 3;

    @Bean
    public DirectExchange rescueExchange() {
        return new DirectExchange("rescue.exchange");
    }

    @Bean
    public DirectExchange delayExchange() {
        return new DirectExchange("delay.exchange");
    }

    // 声明不同范围的队列（1km, 2km, 3km）
    @Bean
    public Queue rescueQueue() {
        return QueueBuilder.durable("rescue.queue.1km")
                .withArgument("x-dead-letter-exchange", "delay.exchange")
                .withArgument("x-dead-letter-routing-key", "rescue.2km")
                .withArgument("x-message-ttl", 10000) // 10秒超时
                .build();
    }

    @Bean
    public Queue retryQueue2km() {
        return QueueBuilder.durable("rescue.queue.2km")
                .withArgument("x-dead-letter-exchange", "delay.exchange")
                .withArgument("x-dead-letter-routing-key", "rescue.3km")
                .withArgument("x-message-ttl", 10000) // 10秒超时
                .build();
    }

    @Bean
    public Queue finalRetryQueue() {
        return QueueBuilder.durable("rescue.queue.3km")
                .withArgument("x-message-ttl", 10000) // 最后一级队列的TTL
                .build();
    }

    // 绑定队列到交换机
    @Bean
    public Binding binding1km(Queue rescueQueue, DirectExchange rescueExchange) {
        return BindingBuilder.bind(rescueQueue).to(rescueExchange).with("rescue.1km");
    }

    @Bean
    public Binding binding2km(Queue retryQueue2km, DirectExchange rescueExchange) {
        return BindingBuilder.bind(retryQueue2km).to(rescueExchange).with("rescue.2km");
    }

    @Bean
    public Binding binding3km(Queue finalRetryQueue, DirectExchange rescueExchange) {
        return BindingBuilder.bind(finalRetryQueue).to(rescueExchange).with("rescue.3km");
    }
}